package com.cn.study.third.stage.codezuoye2;

public class CompareTest {

    static String minStr;

    static String maxStr;

    static String subMinStr;// minStr的子字符串

    public static String test(String str1, String str2) {
// 判断str1和str2哪个是较短的字符串

        if (str1.length() < str2.length()) {
            minStr = str1;

            maxStr = str2;

        } else {
            minStr = str2;

            maxStr = str1;

        }

        System.out.println("maxStr: " + maxStr + ", " + "minStr: " + minStr);

// 找出相同的最长子字符串

        boolean flag=true;

        for (int i = 0; i < minStr.length(); i++) { // i 控制行数

            if (!flag) break;

            System.out.print("subMinStr: ");

            for (int j = 0; j <= i; j++) {// j 控制每一行的列数及str2字符串开始下标

// subStr2是str2所有可能的子字符串

                subMinStr = minStr.substring(j, minStr.length() - i + j); // minStr.length()-i+j 控制str2字符串结束下标

                System.out.print(subMinStr + " ");

                if (maxStr.contains(subMinStr)) { // 如果maxStr包含了minStr或者minStr的子字符串，那么就结束循环

                    flag = false;

                    break;

                }

            }

            System.out.println();

        }

        return subMinStr;

    }

    public static void main (String[]args){
        String MaxSubStr = test("asdafghjka", "aaasdfg");

        System.out.println("相同的最大字符串是：" + MaxSubStr);

    }

}
